<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入考试名称"
        v-model="search"
        style="width: 200px"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>
      <span class="a1">年级：</span>
      <el-select
        v-model="gradeId"
        @change="load()"
        clearable
        placeholder="请选择"
        style="width: 130px"
      >
        <el-option
          v-for="item in gradeInfo"
          :key="item.value"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <div style="margin: 10px 0; float: right; margin-right: 20px">
        <el-button type="primary" @click="add">添加</el-button>
        <el-button type="success" @click="ScoreList" :disabled="disabled"
          >查看成绩</el-button
        >
      </div>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 99%"
      @selection-change="handleSelectionChange"
      height="75vh"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="name" label="考试名称" />
      <el-table-column prop="time" label="考试时间" />
      <el-table-column prop="type1" label="考试类型"> </el-table-column>
      <el-table-column prop="gradeName" label="考试年级" />
      <el-table-column
        prop="remark"
        label="备注"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="未发布/发布" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ccc"
            @change="changeEnable(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="160px">
        <template #default="scope">
          <el-button
            @click="handleEdit(scope.row)"
            size="mini"
            type="primary"
            plain
            >编辑</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="添加考试信息"
      v-model="dialogVisible"
      width="50%"
      :before-close="dialogClose"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-row>
          <el-col span="12">
            <el-form-item label="考试编号" prop="id" label-width="100px">
              <el-input v-model="form.id" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="考试名称" prop="name" label-width="100px">
              <el-input v-model="form.name" placeholder="请输入考试名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="考试年级" prop="gradeId" label-width="100px">
              <el-select
                v-model="form.gradeId"
                placeholder="请选择年级"
                style="width: 190px"
              >
                <el-option
                  v-for="item in gradeInfo"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="考试时间" prop="time" label-width="100px">
              <el-date-picker
                v-model="form.time"
                type="date"
                placeholder="选择日期"
                style="width: 190px"
                :disabled-date="disabledDate"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="考试类型" prop="type" label-width="100px">
              <el-radio v-model="form.type" label="1">年级考试</el-radio>
              <el-radio v-model="form.type" label="2">平时考试</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="24">
            <el-form-item label="备注" prop="remark" label-width="100px">
              <el-input
                type="textarea"
                v-model="form.remark"
                style="width: 400px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="this.dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      title="编辑考试信息"
      v-model="updateVisible"
      width="50%"
      :before-close="dialogClose"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-row>
          <el-col span="12">
            <el-form-item label="考试编号" prop="id" label-width="100px">
              <el-input
                v-model="form.id"
                placeholder="请输入编号"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="考试名称" prop="name" label-width="100px">
              <el-input v-model="form.name" placeholder="请输入考试名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="考试年级" prop="gradeId" label-width="100px">
              <el-select
                v-model="form.gradeId"
                placeholder="请选择年级"
                style="width: 190px"
              >
                <el-option
                  v-for="item in gradeInfo"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="考试时间" prop="time" label-width="100px">
              <el-date-picker
                v-model="form.time"
                type="date"
                placeholder="选择日期"
                style="width: 190px"
                :disabled-date="disabledDate"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="考试类型" prop="type" label-width="100px">
              <el-radio v-model="form.type" :label="1">年级考试</el-radio>
              <el-radio v-model="form.type" :label="2">平时考试</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="24">
            <el-form-item label="备注" prop="remark" label-width="100px">
              <el-input
                type="textarea"
                v-model="form.remark"
                style="width: 400px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="this.updateVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "Score",
  data() {
    return {
      disabledDate(time) {
          return time.getTime() < Date.now()
        },
      rules: {
        id: [
          { required: true, message: "编号不能为空", trigger: "blur" },
        ],
        name:[
          {required: true, message: "考试名称不能为空", trigger: "blur"}
        ],
        gradeId:[
          {required: true, message: "请选择考试年级", trigger: "blur"}
        ],
        time:[
          {required: true, message: "请选择考试时间", trigger: "blur"}
        ],
        type:[
          {required: true, message: "请选择考试类型", trigger: "blur"}
        ]
      },
      //当前页
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      updateVisible: false,
      scoreVisible: false,
      tableData: [],
      scoreDate: [],
      search: "",
      gradeId: "",
      gradeInfo: {},
      form: {},
      examId: "",
      examGradeId: "",
      examName: "",
      disabled: false,
    };
  },
  created() {
    this.load();
    this.findGradeInfo();
  },
  methods: {
    // 修改状态
    changeEnable(row) {
      console.log("1111111111111");
      console.log(row.id);
      console.log(row.status);
      request
        .put("/exam/updateStatus?id=" + row.id + "&status=" + row.status)
        .then((res) => {
          console.log(res);
          if (res.code === "0" && row.status === 1) {
            this.$message({
              type: "success",
              message: "已发布！学生可登录查看成绩信息",
            });
          } else {
            this.$message({
              type: "error",
              message: "取消发布",
            });
          }
          this.load();
          this.updateVisible = false;
        });
    },
    // 分页查询
    load() {
      request
        .get("/exam/all", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            name: this.search,
            gradeId: this.gradeId,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          console.log("=============>");
          console.log(this.tableData);
          for (let i = 0; i <= res.data.records.length; i++) {
            this.tableData[i].gradeName = res.data.records[i].gradeList.name;
            if (res.data.records[i].type == 1) {
              this.tableData[i].type1 = "年级考试";
            } else {
              this.tableData[i].type1 = "平时考试";
            }
          }
        });
    },
    // 查询年级信息
    findGradeInfo() {
      request.get("/grade/all", {}).then((res) => {
        console.log(res);
        this.gradeInfo = res.data;
      });
    },
    // 添加考试信息
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          request.post("/exam/add", this.form).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.dialogVisible = false;
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
            this.load();
          });
        }
      });
    },
    // 更新考试信息
    update() {
      request.put("/exam/update", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
        } else {
          this.$message({
            type: "error",
            message: "更新失败",
          });
        }
        this.load();
        this.updateVisible = false;
      });
    },
    // 删除考试信息
    handleDelete(id) {
      console.log(id);
      this.$confirm("是否删除该考试信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/exam/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //打开添加框
    add() {
      this.dialogVisible = true;
      this.form = {};
    },
    // 打开编辑框
    handleEdit(row) {
      this.updateVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
      console.log(row);
    },
    // 表格单选
    handleSelectionChange(val) {
      if (val.length >= 2) {
        this.disabled = true;
        this.$message({ type: "error", message: "只能选择一门考试" });
        this.examId = "";
      } else {
        this.disabled = false;
        console.log(val[0].id);
        this.examId = val[0].id;
        this.examGradeId = val[0].gradeId;
        this.examName = val[0].name;
        console.log(this.examId);
      }
    },
    ScoreList() {
      if (this.examId == "") {
        this.$message({ type: "error", message: "请选择考试" });
      } else {
        console.log(this.examId);
        console.log("22222222");
        console.log(this.examGradeId);
        this.$router.push({
          path: "adminscore",
          name: "adminscore",
          query: {
            examId: this.examId,
            gradeId: this.examGradeId,
            examName: this.examName,
          },
        });
      }
    },
    //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>

<style>
.a1 {
  font-size: 12px;
  text-align: center;
  margin-left: 20px;
}
</style>
